Imports System
Imports System.Collections
Imports System.Text
Imports System.XML
Imports DotNetNuke.Common.Utilities

Namespace DotNetNuke.Modules.ProjectTracker

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    '''		The ProcessController class provides business layer methods
    '''		for Process objects used by the ProjectTracker module.
    ''' </summary>
    ''' <remarks></remarks>
    ''' <history>
    '''		[MyGeneration]	3/18/2006 2:47:28 PM	Code file automatically generated from DotNetNuke.PT_Process by MyGeneration v1.1.5.1
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class ProcessController

        Implements Entities.Modules.IPortable


        Public Sub New()
            ' default object initialisation	
        End Sub

#Region " Public Methods "

        ''' <summary>
        ''' Adds a new Process record
        ''' </summary>
        ''' <returns>Identifier for the newly created object</returns>
        Public Function AddProcess(ByVal processInfo As ProcessInfo) As Integer
            Return CType(DataProvider.Instance().AddProcess(processInfo.ModuleID, processInfo.CreatedByUser, processInfo.CreatedDate, processInfo.WorkflowName, processInfo.Description), Integer)
        End Function


        ''' <summary>
        ''' Updates a specified Process
        ''' </summary>
        ''' <returns></returns>
        Public Shared Sub UpdateProcess(ByVal processInfo As ProcessInfo)
            DataProvider.Instance().UpdateProcess(processInfo.ItemID, processInfo.ModuleID, processInfo.CreatedByUser, processInfo.CreatedDate, processInfo.WorkflowName, processInfo.Description)
        End Sub


        ''' <summary>
        ''' Deletes a specified Process
        ''' </summary>
        ''' <returns></returns>
        Public Shared Sub DeleteProcess(ByVal itemID As Integer)
            DataProvider.Instance().DeleteProcess(itemID)
        End Sub


        ''' <summary>
        ''' Retrieves the details of a specified Process
        ''' </summary>
        ''' <returns>ProcessInfo object</returns>
        Public Function GetProcessInfo(ByVal itemID As Integer) As ProcessInfo
            Return CType(CBO.FillObject(DataProvider.Instance().GetProcessInfo(itemID), GetType(ProcessInfo)), ProcessInfo)
        End Function


        ''' <summary>
        ''' Retrieves the entire list of Processes
        ''' </summary>
        ''' <returns>ArrayList of ProcessInfo objects</returns>
        Public Function GetProcessList(ByVal moduleID As Integer) As ArrayList
            Return CBO.FillCollection(DataProvider.Instance().GetProcessList(moduleID), GetType(ProcessInfo))
        End Function


#End Region

#Region "Optional Interfaces"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ExportModule implements the IPortable ExportModule Interface
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="ModuleID">The Id of the module to be exported</param>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements Entities.Modules.IPortable.ExportModule
            Dim strXML As New StringBuilder

            Dim arrList As ArrayList = Me.GetProcessList(ModuleID)
            If arrList.Count <> 0 Then
                strXML.Append("<processes>")
                For Each objItem As ProcessInfo In arrList
                    strXML.Append("<process>")
                    strXML.Append("<itemid>XMLEncode(objItem.ItemID.ToString())</itemid>")
                    strXML.Append("<moduleid>XMLEncode(objItem.ModuleID.ToString())</moduleid>")
                    strXML.Append("<createdbyuser>XMLEncode(objItem.CreatedByUser.ToString())</createdbyuser>")
                    strXML.Append("<createddate>XMLEncode(objItem.CreatedDate.ToString())</createddate>")
                    strXML.Append("<workflowname>XMLEncode(objItem.WorkflowName.ToString())</workflowname>")
                    strXML.Append("<description>XMLEncode(objItem.Description.ToString())</description>")
                    strXML.Append("</process>")
                Next
                strXML.Append("</processes>")
            End If

            Return strXML.ToString()

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ImportModule implements the IPortable ImportModule Interface
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="ModuleID">The Id of the module to be imported</param>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserId As Integer) Implements Entities.Modules.IPortable.ImportModule
            Dim xmlList As XmlNode = GetContent(Content, "processes")
            For Each xmlItem As XmlNode In xmlList.SelectNodes("process")
                Dim objItem As New ProcessInfo
                objItem.ItemID = CType(Null.GetNull(xmlItem.Item("itemid").InnerText, Null.NullInteger), Integer)
                objItem.ModuleID = CType(Null.GetNull(xmlItem.Item("moduleid").InnerText, Null.NullInteger), Integer)
                objItem.CreatedByUser = CType(Null.GetNull(xmlItem.Item("createdbyuser").InnerText, Null.NullInteger), Integer)
                objItem.CreatedDate = CType(Null.GetNull(xmlItem.Item("createddate").InnerText, Null.NullDate), DateTime)
                objItem.WorkflowName = CType(Null.GetNull(xmlItem.Item("workflowname").InnerText, Null.NullString), String)
                objItem.Description = CType(Null.GetNull(xmlItem.Item("description").InnerText, Null.NullString), String)

                If objItem.ItemID = Null.NullInteger Then
                    Me.AddProcess(objItem)
                Else
                    Me.UpdateProcess(objItem)
                End If
            Next
        End Sub

#End Region


    End Class

End Namespace
